import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;

import javax.swing.Box;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.border.EmptyBorder;
import javax.swing.JCheckBox;
import java.awt.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JSpinner;
import javax.swing.JScrollPane;


public class Charger extends JFrame implements ActionListener {

	private JPanel contentPane;
	private JPanel contentPane1;

	/**
	 * Launch the application.
	 */
	private JLabel[] label; 
	private JRadioButton[] cases; 
	private int k=0;
	private JLabel label5;
	private JLabel label1;
	private JLabel label2;
	private JLabel label3;
	private JLabel label4;
	private JLabel label8;
	private JLabel label6;
	private JLabel label7;
	private JLabel label9;
	private JLabel label10;
	private Box b2=Box.createVerticalBox();
	private Box b1=Box.createVerticalBox();

	//private JScrollPane barre;
	private  JScrollPane scrollPane = new JScrollPane(b1);
	
	
	 private Bouton bouton1=new Bouton("Terminer");
	 private Bouton bouton2=new Bouton("Annuler"); 
		
	 private QCM qcm;
	 
	 private int reponsejuste=0;
	 private int reponsefausse=0;
	 private int nbrepjuste=0;
	 
	
     
	 

	/**
	 * Create the frame.
	 */
	public Charger(QCM qcm) {
		this.qcm=qcm;
		//setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//setBounds(100, 100, 600, 600);
		 this.setSize(600,600);
			
		this.setLocationRelativeTo(null);
        setTitle(qcm.getNomQCM());
		setResizable(false);
		contentPane = new JPanel();
		//contentPane1 = new JPanel();
		//contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		//contentPane.setLayout(new FlowLayout());
		//contentPane1.setLayout(new FlowLayout());
		
		setContentPane(b2);
		
		
		//contentPane.add(contentPane1);
		
		  label=new JLabel[20]; 
          cases=new JRadioButton[10];
          
          //Font police=new Font("Tahoma",Font.BOLD,22);
          label4=new JLabel("\n");
          label1=new JLabel("                                                                                  "+qcm.getNomQCM());
          label2=new JLabel("\n");
          label3=new JLabel("\n");
          //b1.add(contentPane1);
         // b1.add(contentPane1);
          //co.add(b2);
          //b1.add(b2);
          b1.add(label1);
          b1.add(label2);
          b1.add(label3);
          
          
           b2.add(new JScrollPane(b1),BorderLayout.CENTER);
           
           
          
          
          //b1.add(barre,BorderLayout.EAST);
          
                 for(int i=0;i<qcm.getNombreQuestion();i++)
		   {
                	 label10=new JLabel("\n");
                	 b1.add(label10);
                	 
	    	   System.out.println(i);
	    	   System.out.println(qcm.getNombreQuestion());
	    	   
	    	   System.out.println(qcm.getTab(i).getQuestion());
	    	   
	    	   label5=new JLabel("Question:"+(i+1));
	    	   b1.add(label5);
	    	   //label6=new JLabel("\n");
	    	   label7=new JLabel("\n");
	    	  // b1.add(label6);
	    	   b1.add(label7);
	           
	    	   label[i]=new JLabel(qcm.getTab(i).getQuestion());
			  System.out.println("Question"+i+label[i].getText());
			  b1.add(label[i]);
			  //label8=new JLabel("\n");
			  label9=new JLabel("\n");
			  //b1.add(label8);
			  b1.add(label9);
			  
			  
			   for(int j=0;j<5;j++)
			   {
				   if(!(qcm.getTab(i).getReponse(j).getRep().equals("Sans Titre") ||
						   qcm.getTab(i).getReponse(j).getRep().equals("" )))
				   {
					   System.out.println("les reponses "+k);
					   cases[k]=new JRadioButton("");
					   cases[k]=new JRadioButton(qcm.getTab(i).getReponse(j).getRep());

					  System.out.println("Reponses"+k+cases[k].getText());
					  b1.add(cases[k]);
					  //b2.add(contentPane);
					  k++;
						 
					  }
				    
				   
					   
			   }
			   
		   }
                 
                 b2.add(contentPane);
                 contentPane.add(bouton1);
                 contentPane.add(bouton2);
                 
                 bouton1.addActionListener(this);
         		 bouton2.addActionListener(this);
         		
	}   
	
                 public void actionPerformed(ActionEvent arg0) {
          		   if (arg0.getSource()==bouton2)
          		   {
          			    this.setVisible(false);
          			   
          			    }
          		 if (arg0.getSource()==bouton1)
        		   {
          			 
        			    this.setVisible(false);
        			    
        			     int s=0;
        			    for (int i=0;i<qcm.getNombreQuestion();i++)
        			    {
        			    	s=0;
        			    	for (int j=0;j<5;j++)
        			    	{
        			    		if (!(qcm.getTab(i).getReponse(j).getRep().equals("Sans Titre") || qcm.getTab(i).getReponse(j).getRep().equals("")))
        			    		{
        			    			if (qcm.getTab(i).getReponse(j).isRepJuste())
        			    			{
        			    				
        			    				nbrepjuste++;
        			    				
        			    				while(s<k)
        			    				{
        			    				
        			    					if (cases[s].getText().equals(qcm.getTab(i).getReponse(j).getRep()))
        			    					{
        			    						if (cases[s].isSelected())
        			    						{
        			    							reponsejuste++;
        			    						}
        			    						else 
        			    						{
        			    							reponsefausse++;
        			    							
        			    						}
        			    						
        			    					}
        			    					s++;
        			    				}
        			    		
        			    			}
        			    			
        			    			
        			    		}
        			    	}
        			    	}
        			    System.out.println("les reponses justes"+reponsejuste);

        			    System.out.println("les reponses fausses"+reponsefausse);
        			    
        			    System.out.println("toutes les reponses"+nbrepjuste);
	    				
        			    				
        			   
        			   System.out.println("yassine");
        		   }
                 }
          	
                 
		   

	

}
